#!/bin/bash
PYTHON_DIR=$3
TESTFILE=$1
TIMEOUT=$2
OUTPUT_FILE="$PYTHON_DIR/__list_output.txt"
CURRENT_PATH=$( cd "$( dirname "$0" )" && pwd )

mkdir "$PYTHON_DIR/__list_output" 2> /dev/null
chgrp testovac "$PYTHON_DIR/__list_output" 2> /dev/null
chmod g*rwx "$PYTHON_DIR/__list_output" 2> /dev/null
chgrp testovac "$PYTHON_DIR/." 2> /dev/null
chmod g+r "$PYTHON_DIR/." 2> /dev/null

cd $PYTHON_DIR

# STAGE 1 Run:
echo "<pre>" > $OUTPUT_FILE
echo "Stage 1 / 1: running test ..." >> $OUTPUT_FILE
echo "----- Running test file Test$TESTFILE.py<br />" >> $OUTPUT_FILE
#cp $CURRENT_PATH/../docker_source/python/*.py $PYTHON_DIR/.
cp -r $PYTHON_DIR/* $PYTHON_DIR/__list_output 2> /dev/null
rm -f $PYTHON_DIR/__list_output/__list_output.txt 2> /dev/null
rm -f $PYTHON_DIR/__list_encrypt_phrase.txt 2> /dev/null
chgrp -R testovac $PYTHON_DIR/__list_output 2> /dev/null
chmod g+w $PYTHON_DIR/__list_output 2> /dev/null
cd "$PYTHON_DIR/__list_output"
sudo -u testovac "$CURRENT_PATH/execute_pyUnit_test" $TESTFILE $TIMEOUT $CURRENT_PATH >> $OUTPUT_FILE 2>> $OUTPUT_FILE
exitcode=$?
cp -f $PYTHON_DIR/__list_output/__list_score.txt $PYTHON_DIR/__list_score.txt 2> /dev/null
cd $CURRENT_PATH
echo "----- Testing finished.</pre>" >> $OUTPUT_FILE
exit $exitcode
